<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}">title</title>
    <link rel="stylesheet" href="/css/milligram.min.css"/>
</head>
<body>

<input id="uid" type="hidden" th:value="${uid}"/>

<form onsubmit="return false;">
    <h3>WebSocket 聊天室：</h3>
    <p>输入 "uid:内容" 和指定的人聊天</p>
    <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
    <br>
    <input type="text" name="message" style="width: 300px" value="">
    <input type="button" value="发送消息" onclick="sendMessage(this.form.message.value)">
    <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空聊天记录">
</form>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {

        axios.get('/chat/node').then(function (res) {
            var data = res.data;
            if (data.unavailable) {
                alert("未找到Netty Server 服务");
            } else {
                establish(data.nodeUrl);
            }
        }, function (res) {
            alert("获取节点出错，请检查请求地址是否正确");
        });


    } else {
        alert("你的浏览器不支持 WebSocket！");
    }

    function establish(url) {
        socket = new WebSocket(url);
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            var data = eval("(" + event.data + ")");
            if (data.action != 'Login' && data.uid != document.getElementById("uid").value) {
                ta.value = ta.value + '\n' + '[' + data.uid + '] : ' + data.message;
            }
        };
        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "连接开启!";
            send(JSON.stringify({"action": "Login", "uid": document.getElementById("uid").value}))

        };
        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "连接被关闭";
        };
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("连接没有开启.");
        }
    }

    function sendMessage(message) {
        if (message) {
            var arr = message.split(":");
            var msg = {uid: document.getElementById('uid').value};
            if (arr.length > 1) {
                msg.action = "Talk";
                msg.targetUid = arr[0];
                msg.message = arr[1];
            } else {
                msg.action = "Broadcast"
                msg.message = arr[0];
            }

            var ta = document.getElementById('responseText');
            ta.value = ta.value + "\n" + "[you] :" + msg.message;

            send(JSON.stringify(msg));
        }
    }
</script>
</body>
</html>